﻿using System;
using System.Linq;
using System.Net;

namespace OmniKits
{
    using Utility = Utilities.EasyHttpUtility;

    partial class Helpers
    {
        public sealed class EasyHttpHelper { }
        public static EasyHttpHelper EasyHttp { get; }

        public static HttpWebRequest Open(this EasyHttpHelper _, string uri)
            => Utility.Open(uri);
        public static HttpWebRequest Open(this EasyHttpHelper _, Uri uri)
            => Utility.Open(uri);
    }
}

namespace OmniKits.Utilities
{
    public static class EasyHttpUtility
    {
#pragma warning disable 1591

        private static readonly string[] ValidSchemes = new[] { "HTTP", "HTTPS" };

        public static HttpWebRequest Open(string uri)
        {
            Uri tmp;
            try
            {
                tmp = new Uri(uri);
            }
            catch (Exception ex)
            {
                throw new ArgumentException(null, nameof(uri), ex);
            }

            return Open(tmp);
        }

        public static HttpWebRequest Open(Uri uri)
        {
            if (!ValidSchemes.Contains(uri.Scheme.ToUpperInvariant()))
                throw new ArgumentException(null, nameof(uri));

            var request = (HttpWebRequest)WebRequest.Create(uri);
            return request;
        }
    }
}
